package org.water.garden.activity.chat;

import org.water.garden.R;
import org.water.garden.core.Intents;
import org.water.garden.receiver.MessageReceiver;
import org.water.garden.view.ChatListAdapter;

import android.app.ListActivity;
import android.app.NotificationManager;
import android.content.ComponentName;
import android.content.ContentUris;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Im.Chats;
import android.view.Menu;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * @author sshwsfc@gmail.com
 * Display All im Chats
 */
public class ChatList extends ListActivity {

	public static final int DELETE_ID = Menu.FIRST;
	
	private Cursor cursor;
	private Uri url;

	@Override
	protected void onCreate(Bundle icicle) {
		super.onCreate(icicle);

		setDefaultKeyMode(SHORTCUT_DEFAULT_KEYS);

		Intent intent = getIntent();
		if (intent.getData() == null) {
			intent.setData(Chats.CONTENT_URI);
		}
		
		cursor = Chats.query(getContentResolver(), new String[]{
			Chats._ID, Chats.CONTACT_ID
		});
		
		ListAdapter adapter = new ChatListAdapter(this, cursor);
		setListAdapter(adapter);
	}

	@Override
	protected void onResume() {
		super.onResume();
		
		cursor.requery();
		NotificationManager notifMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
		notifMgr.cancel(MessageReceiver.NOTFI_CODE_MESSAGE);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);

		Intent intent = new Intent(null, getIntent().getData());
		intent.addCategory(Intent.ALTERNATIVE_CATEGORY);
		menu.addIntentOptions(Menu.ALTERNATIVE, 0, new ComponentName(this, ChatList.class), null, intent, 0, null);

		menu.add(Menu.SELECTED_ALTERNATIVE, DELETE_ID, R.string.account_delete, R.drawable.account_remove).setShortcut('2', 'd');
		return true;
	}

	@Override
	public boolean onPrepareOptionsMenu(Menu menu) {
		super.onPrepareOptionsMenu(menu);
		final boolean haveItems = cursor.count() > 0;

		if (!haveItems) {
			menu.removeGroup(Menu.SELECTED_ALTERNATIVE);
		}

		menu.setItemShown(DELETE_ID, haveItems);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(Menu.Item item) {
		switch (item.getId()) {
		case DELETE_ID:
			deleteChat();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {		
		url = ContentUris.withAppendedId(Chats.CONTENT_URI, id);

		String action = getIntent().getAction();
		if (Intent.PICK_ACTION.equals(action) || Intent.GET_CONTENT_ACTION.equals(action)) {
			setResult(RESULT_OK, url.toString());
		} else {
			Intent menuChatsInt = new Intent(Intents.VIEW_CHAT, url);
			startActivity(menuChatsInt);
		}
	}
	
	private final void deleteChat() {
		if(getSelectedItemPosition() >= 0){
			cursor.moveTo(getSelectedItemPosition());
			cursor.deleteRow();
		}
	}
	
}
